<?php
/**
 *
 * @author andrew carlson <andrew@acstudiosllc.com>
 * @version 0.0.1
 * @copyright 2012 ACStudios LLC
 * @package default
 */

/**
 * General Utilities class for common functions
 * shouldn't need to extend the model or anything
 * no serious functions, just common tasks that
 * can be reused
 */

namespace Encompass\Utilities;
use PDO;

/**
 * cmUtilities class
 *
 * @package default
 * @author  andrew carlson <andrew@acstudiosllc.com>
 */
 class utilities {

	/**
	 * params class variable - all URL/GET/POST parameters
	 *
	 * @var string $this->params
	 */
	var $params = '';

	/**
	 * convert url parameters to an array, may remove this or turn it into an object
	 */
    function getParams() {
		return ($this->params = $_REQUEST);
    }
	
	/**
	 * get the 1st option after the domain and look for the proper controller
	 * and load it.  Would like to get away from having this hard coded
	 */
	function controlRoute() {
		$url = explode("/", $_SERVER['REQUEST_URI']);
		$controller = (empty($url[1])) ? 'core' : $url[1]; 
		$controllerPath = ucfirst($controller);
		$fullNameSpace = "\\".ENV."\\$controllerPath\\".$controller."Controller";

		$ctl = new $fullNameSpace;
	}
	
	/**
	 * look for the second option in the url as the action (what are we doing)
	 * and return it.  Actions are defined inside controllers
	 */
	function actionRoute() {
		$url = explode ("/", $_SERVER['REQUEST_URI']);
		$controller = $url[1];
		$action = (empty($url[2])) ? 'grid' : $url[2];
		
		if (preg_match('/\?/', $action)) {
			$newAction = explode("?", $action);
			return $newAction[0];
		}

		return $action;
	}
	
	/**
	 * buildMenus function scans the class for all methods, any method with the
	 * word public_ is determined to be a menu and returned.
	 *
	 * @param object $class 
	 * @return void
	 * @author  andrew carlson <andrew@acstudiosllc.com>
	 */
	function buildMenus($class) {
		// get modules first
		$moduleMenu = \Encompass\Utilities\utilities::getModules();

		// get class functions as menu items
		$matches = preg_grep("/public_/", $class);
		$matchesOptions = str_replace("public_", "", $matches);
	
		$menu = array_merge($moduleMenu, $matchesOptions);

		return ($menu);
	}	

	/**
	 * get list of modules and add them to the current menu
	 */
	function getModules() {
		$sql = "SELECT module_name FROM modules";

		\Encompass\Utilities\utilities::dbConnect();
		$query = $this->db->prepare($sql);
		
		try {
			$query->execute();
		} catch (PDOException $e) {
			throw new Exception ("PDO Error " . $e->getMessage());
		}
		
		$results = $query->fetchAll(PDO::FETCH_ASSOC);

		foreach ($results as $module) {
			$menu[] = $module['module_name'];
		}

		return $menu;
	}
	
	/**
	 * simple db connector (PDO) until the full pdo IF is done
	 */
	function dbConnect() {
        try {
            return $this->db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
        } catch (PDOException $e) {
            //echo "Failed to connect to database " . $e -> getMessage();
            die ;
        }		
	}		
 }
?>  
